package com.liu.sx.service;

import com.liu.sx.common.util.IdWorker;
import com.liu.sx.dao.DriverLoginDao;
import com.liu.sx.entity.Driver;
import com.liu.sx.entity.DriverLogin;
import com.liu.sx.pojo.BW;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;

@Service
@Slf4j
@Transactional
public class DriverLoginService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private DriverLoginDao driverLoginDao;

    public void  addLogin(DriverLogin driverLogin){

        driverLogin.setId(idWorker.nextId()+"");
        driverLogin.setLoginTime(new Date());
        driverLoginDao.save(driverLogin);
    }

    public void insert(BW bw,String romteIp){
        DriverLogin driverLogin = new DriverLogin();
        driverLogin.setAddress(bw.getAddress());
        driverLogin.setRomteIp(romteIp);
        addLogin(driverLogin);

    }

    public Page<DriverLogin> findAll(int page, int rows, String query) {
        Pageable pageable = PageRequest.of(page-1,rows, Sort.Direction.DESC,"loginTime");
        return  driverLoginDao.findAll(query,pageable);

    }
}
